package com.omesoft.hksnore.util.data;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Base64;

import com.omesoft.hksnore.util.L;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

/**
 * @autor omesoft_lgc
 * <p>
 * create at 2017/2/16 15:09
 */
public class MySharedPreferencesUtil {
    public final static String PROJECTNAME = "funiatens";

    public static SharedPreferences getProjectSP(Context context) {
        return context.getSharedPreferences(PROJECTNAME, Context.MODE_PRIVATE);
    }

    /**
     * 本地 时间戳的存储
     */
    public static boolean setLocalTime(Context context, String time) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        return editor.putString("local_time", time).commit();
    }

    /**
     * 得到本地时间戳
     */
    public static String getLocalTime(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getString("local_time", "1980-01-01 00:00:00");
    }
    /**
     * 本地 时间戳的存储
     */
    public static boolean setUpdateTime(Context context, String time) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        return editor.putString("update_time", time).commit();
    }

    /**
     * 得到本地时间戳
     */
    public static String getetUpdateTime(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getString("update_time", "1980-01-01 00:00:00");
    }

    /**
     * 设置服务器的时间戳
     */
    public static boolean setBodyTime(Context context, String time) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        return editor.putString("bodyTime", time).commit();
    }

    /**
     * 得到服务器时间戳
     */
    public static String getBodyTime(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getString("bodyTime", "0");
    }

    /**
     * 获取用户登录名
     *
     * @param context
     * @return
     */
    public static String getUserName(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getString("userName", null);
    }

    /**
     * 设置用户登录名
     *
     * @param context
     * @param userName
     */
    public static boolean setUserName(Context context, String userName) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        return editor.putString("userName", userName).commit();
    }

    /**
     * 获取用户登录密码
     *
     * @param context
     * @return
     */
    public static String getPassWord(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getString("passWord", null);
    }

    /**
     * 设置用户登录密码
     *
     * @param context
     * @param passWord
     */
    public static boolean setPassWord(Context context, String passWord) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        return editor.putString("passWord", passWord).commit();
    }

    /**
     * 获取用户钥匙
     *
     * @param context
     * @return
     */
    public static String getCilenKey(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getString("clientKey", null);
    }

    /**
     * 设置用户钥匙
     *
     * @param context
     * @param clientKey
     */
    public static boolean setCilenKey(Context context, String clientKey) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        return editor.putString("clientKey", clientKey).commit();
    }

    /**
     * 设置用户ID
     *
     * @param context
     * @param menberId
     */
    public static boolean setMemberId(Context context, int menberId) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        return editor.putInt("memberId", menberId).commit();
    }
    /**
     * 获取用户ID
     *
     * @param context
     * @return
     */
    public static int getMemberId(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getInt("memberId", -1);
    }
    /**
     * 设置用户ID
     *
     * @param context
     * @param familyId
     */
    public static boolean setFamilyId(Context context, int familyId) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        return editor.putInt("familyId", familyId).commit();
    }

    /**
     * 用于临时保存dto数组的大小，以监测是否有新数据插入
     *
     * @param context
     * @return
     */
    public static int getListSize(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getInt("ListSize", 0);
    }
    /**
     * 用于临时保存dto数组的大小，以监测是否有新数据插入
     *
     * @param context
     * @param ListSize
     */
    public static boolean setListSize(Context context, int ListSize) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        return editor.putInt("ListSize", ListSize).commit();
    }

    /**
     * 获取用户ID
     *
     * @param context
     * @return
     */
    public static int getFamilyId(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getInt("familyId", -1);
    }

    /**
     * 设置用户昵称
     * @param context
     * @param name
     */
    public static boolean setName(Context context, String name) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        return editor.putString("name", name).commit();
    }

    /**
     * 获取用户昵称
     *
     * @param context
     * @return
     */
    public static String getName(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getString("name", null);
    }


    /**
     * 设置mac
     * @param context
     * @param mac
     */
    public static boolean setMac(Context context, String mac) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        return editor.putString("mac", mac).commit();
    }

    /**
     * 获取mac
     *
     * @param context
     * @return
     */
    public static String getMac(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getString("mac", null);
    }
    /**
     * 设置临时保存用于画图的SlieepID
     * @param context
     * @param SleepId
     */
    public static boolean setTempSleepID(Context context, String SleepId) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        return editor.putString("SleepID", SleepId).commit();
    }

    /**
     *
     * @param context
     * @return sleepID
     */
    public static String getTempStartTime(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getString("TempStartTime", null);
    }
    /**
     * 设置临时保存用于画图的监测开始时间
     * @param context
     * @param TempStartTime
     */
    public static boolean setTempStartTime(Context context, String TempStartTime) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        return editor.putString("TempStartTime", TempStartTime).commit();
    }
    /**
     *
     * @param context
     * @return sleepID
     */
    public static String getTempEndTime(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getString("TempEndTime", null);
    }
    /**
     * 设置临时保存用于画图的监测结束时间
     * @param context
     * @param TempEndTime
     */
    public static boolean setTempEndTime(Context context, String TempEndTime) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        return editor.putString("TempEndTime", TempEndTime).commit();
    }

    /**
     *
     * @param context
     * @return
     */
    public static String getTempSleepID(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getString("SleepID", null);
    }
    /**
     * 设置头像图片地址
     *
     * @param context
     * @param clientKey
     */
    public static boolean setPhotoUrl(Context context, String clientKey) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        return editor.putString("photoUrl", clientKey).commit();
    }

    /**
     * 获取图像图片地址
     *
     * @param context
     * @return
     */
    public static String getPhotoUrl(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getString("photoUrl", null);
    }

    /**
     * 判断用户是否登录，用户登录名和密码是否等于空
     */
    public static boolean isLoginIn(Context context) {
        String clientKey = getCilenKey(context);
        int memberId = getMemberId(context);
        if (clientKey != null && memberId != -1) {
            L.e("MySharedPreferencesUtil", " 登陆了");
            return true;
        } else {
            L.e("MySharedPreferencesUtil", " 没有 登陆");
            return false;
        }
    }


    public static void cleanSp(Context context) {
        onClear(context);
        setCilenKey(context, null);
        setMemberId(context, -1);
        setPhotoUrl(context,null);
        setName(context,null);

        setBodyTime(context, "0");
        setLocalTime(context, "1980-01-01 00:00:00");
    }

    public static void onClear(Context context) {
        // TODO Auto-generated method stub
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        editor.clear();
        editor.commit();
    }

    public static void setTempData() {

    }

    /**
     * 根据key和预期的value类型获取value的值
     *
     * @param key
     * @param clazz
     * @return
     */
    public static <T> T getValue(Context context, String key, Class<T> clazz) {
        if (context == null) {
            throw new RuntimeException("请先调用带有context，name参数的构造！");
        }
        SharedPreferences sp = getProjectSP(context);
        return getValue(key, clazz, sp);
    }

    /**
     * 针对复杂类型存储<对象>
     *
     * @param key
     * @param object
     */
    public static void setObject(Context context, String key, Object object) {
        SharedPreferences sp = getProjectSP(context);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = null;
        try {

            out = new ObjectOutputStream(baos);
            out.writeObject(object);
            String objectVal = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
            Editor editor = sp.edit();
            editor.putString(key, objectVal);
            editor.commit();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 设置震动状态
     *
     * @param context
     * @param flg
     */
    public static void setSwitch(Context context, boolean flg) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        editor.putBoolean("switch", flg).commit();
    }

    public static boolean getSwitch(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getBoolean("switch", true);
    }

    public static void remove(Context context, String key) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        editor.remove(key);
        editor.commit();
    }
    /**
     * 设置震动强度 -1 0 1
     *
     * @param context
     * @param
     */
    public static void setStrength(Context context, int flg) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        editor.putInt("strength", flg).commit();
    }

    public static int getStrength(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getInt("strength", 1);
    }
    /**
     *  保存震动等级
     *
     * @param context
     * @param
     */
    public static void setVbLv(Context context, int lv) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        editor.putInt("vblv", lv).commit();
    }

    public static int getVbLv(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getInt("vblv", 1);
    }
    /**
     *  保存震动等级
     *
     * @param context
     * @param
     */
    public static void setMaxVbLv(Context context, int lv) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        editor.putInt("maxVbLv", lv).commit();
    }

    public static int getMaxVbLv(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getInt("maxVbLv", 1);
    }
    /**
     *  保存入睡时间位置
     *
     * @param context
     * @param
     */
    public static void setInSleepTime(Context context, int position) {
        SharedPreferences sp = getProjectSP(context);
        Editor editor = sp.edit();
        editor.putInt("InSleepTime", position).commit();
    }

    public static int getInSleepTime(Context context) {
        SharedPreferences sp = getProjectSP(context);
        return sp.getInt("InSleepTime", 0);
    }

    @SuppressWarnings("unchecked")
    public static <T> T getObject(Context context, String key, Class<T> clazz) {
        SharedPreferences sp = getProjectSP(context);
        if (sp.contains(key)) {
            String objectVal = sp.getString(key, null);
            byte[] buffer = Base64.decode(objectVal, Base64.DEFAULT);
            ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(bais);
                T t = (T) ois.readObject();
                return t;
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bais != null) {
                        bais.close();
                    }
                    if (ois != null) {
                        ois.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 对于外部不可见的过渡方法
     *
     * @param key
     * @param clazz
     * @param sp
     * @return
     */
    @SuppressWarnings("unchecked")
    private static <T> T getValue(String key, Class<T> clazz, SharedPreferences sp) {
        T t;
        try {

            t = clazz.newInstance();

            if (t instanceof Integer) {
                return (T) Integer.valueOf(sp.getInt(key, 0));
            } else if (t instanceof String) {
                return (T) sp.getString(key, "");
            } else if (t instanceof Boolean) {
                return (T) Boolean.valueOf(sp.getBoolean(key, false));
            } else if (t instanceof Long) {
                return (T) Long.valueOf(sp.getLong(key, 0L));
            } else if (t instanceof Float) {
                return (T) Float.valueOf(sp.getFloat(key, 0L));
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
            L.e("system", "类型输入错误或者复杂类型无法解析[" + e.getMessage() + "]");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            L.e("system", "类型输入错误或者复杂类型无法解析[" + e.getMessage() + "]");
        }
        L.e("system", "无法找到" + key + "对应的值");
        return null;
    }
}
